﻿#region Using Directives

    using System;
    using System.Collections.Generic;  
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    using Csla;
    using Csla.Data;
    using Csla.Server;
    using Csla.Reflection;

    using MarketPulse.BusinessLogic.Core;
    using MarketPulse.DataAccess;
    using MarketPulse.Reporting.Core;
    using MarketPulse.Reporting.Core.Criteria;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    /// <summary>
    /// Factory class for producing ReportingBranchInfoLists
    /// </summary>
    public class ReportingBranchFactory : ObjectFactory
    {
        #region Methods

            #region Fetch Methods

                #region ReportingBranchInfoList

                    /// <summary>
                    /// Method used to get a read only list of read only Reporting Branches that are associated with a particular set of criteria (via the DataPortal client proxy)
                    /// </summary>
                    /// <param name="criteria">The criteria to use to retrieve the list</param>
                    /// <returns>A populated read only list of read only Reporting Branches</returns>
                    public ReportingBranchInfoList FetchInfoList(ClientGUIDAndProfileGUIDAndReportingPeriodAndReportingLevelsCriteria criteria)
                    {
                        try
                        {
                            // Hold onto the current list of reportinglevel branches
                            var reportingLevelBranches = criteria.ReportingLevels.SelectedBranches;

                            using (var manager = ContextManager<DataContextObjects.Production.MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an empty instance of ReportingBranchInfoList to be returned.
                                var reportingBranchInfoList = (ReportingBranchInfoList)MethodCaller.CreateInstance(typeof(ReportingBranchInfoList));

                                // Mark the new instance of ReportingBranchInfoList as writable as it inherits from MarketPulseReadOnlyBase
                                SetIsReadOnly(reportingBranchInfoList, false);

                                // Select the resultset from the database.
                                var reportingBranchInfoSelect = manager.DataContext.UspBranchSelectByClientGUIDAndProfileGUIDAndDateRange(criteria.ClientGUID, criteria.ProfileGUID, criteria.ReportingPeriod.EffectiveStartDateTime, criteria.ReportingPeriod.EffectiveEndDateTime, true);
                                //var reportingBranchInfoSelect = manager.DataContext.UspBranchSelectByClientGUIDAndDateRange(criteria.ClientGUID, criteria.ReportingPeriod.EffectiveStartDateTime, criteria.ReportingPeriod.EffectiveEndDateTime, true);

                                // Iterate over each row and append the object to the ReportingBranchInfoList
                                foreach (var result in reportingBranchInfoSelect)
                                {
                                    // Check if the Branch is in our reporting level
                                    if (!reportingLevelBranches.Contains(result.BranchGUID.Value)) continue;

                                    // Create an empty instance of ReportingBranchInfo to be appended to the ReportingBranchInfoList
                                    var reportingBranchInfo = (ReportingBranchInfo)MethodCaller.CreateInstance(typeof(ReportingBranchInfo));

                                    // Load the base properties
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.GUIDProperty, result.BranchGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.InsertDateTimeProperty, (SmartDate)result.InsertDateTime);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.UpdateDateTimeProperty, (SmartDate)result.UpdateDateTime);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);

                                    // Load the ReportingBranchInfo specific properties

                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.BranchNameProperty, result.BranchName);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.BuildingNameOrNumberProperty, result.BuildingNameOrNumber);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine1Property, result.AddressLine1);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine2Property, result.AddressLine2);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine3Property, result.AddressLine3);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine4Property, result.AddressLine4);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine5Property, result.AddressLine5);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.PostCodeProperty, result.PostCode);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.MemoProperty, result.Memo);

                                    if (result.AreaGUID.HasValue)
                                    {
                                        LoadProperty(reportingBranchInfo, ReportingBranchInfo.AreaGUIDProperty, result.AreaGUID);
                                    }

                                    if (result.CompanyGUID.HasValue)
                                    {
                                        LoadProperty(reportingBranchInfo, ReportingBranchInfo.CompanyGUIDProperty, result.CompanyGUID);
                                    }

                                    if (result.ClientGUID.HasValue)
                                    {
                                        LoadProperty(reportingBranchInfo, ReportingBranchInfo.ClientGUIDProperty, result.ClientGUID);
                                    }

                                    if (result.LegacyBranchID.HasValue)
                                    {
                                        LoadProperty(reportingBranchInfo, ReportingBranchInfo.LegacyBranchIDProperty, result.LegacyBranchID);
                                    }

                                    if (result.LegacyBranchID.HasValue)
                                    {
                                        LoadProperty(reportingBranchInfo, ReportingBranchInfo.LatitudeProperty, result.Latitude);
                                    }

                                    if (result.LegacyBranchID.HasValue)
                                    {
                                        LoadProperty(reportingBranchInfo, ReportingBranchInfo.LongitudeProperty, result.Longtitude);
                                    }


                                    // Mark the ReportingBranchInfo object as old so CSLA knows it has changed
                                    MarkOld(reportingBranchInfo);

                                    // Add the ReportingBranchInfo object to the ReportingBranchInfoList collection
                                    reportingBranchInfoList.Add(reportingBranchInfo);
                                }

                                // Mark the new instance of ReportingBranchInfoList as read-only before returning it to the client
                                SetIsReadOnly(reportingBranchInfoList, true);

                                // Mark the ReportingBranchInfoList object as old so CSLA knows it has changed
                                MarkOld(reportingBranchInfoList);

                                // Return the newly populated ReportingBranchInfoList
                                return reportingBranchInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            //TODO log exception appropriately before re throwing
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

                    /// <summary>
                    /// Method used to get a read only list of read only Reporting Branches that are associated with a particular set of criteria (via the DataPortal client proxy)
                    /// </summary>
                    /// <param name="criteria">The criteria to use to retrieve the list</param>
                    /// <returns>A populated read only list of read only Reporting Branches</returns>
                    public ReportingBranchInfoList FetchInfoList(ClientGUIDAndCountryGUIDAndResultsDateRangeCriteria criteria)
                    {
                        try
                        {
                            using (var productionManager = ContextManager<DataContextObjects.Production.MUM_dev>.GetManager(Database.BusinessSystem))
                            using (var reportingManager = ContextManager<DataContextObjects.Reporting.MUMReporting_dev>.GetManager(Database.ReportingSystem))
                            {
                                // Create an empty instance of ReportingBranchInfoList to be returned.
                                var reportingBranchInfoList = (ReportingBranchInfoList)MethodCaller.CreateInstance(typeof(ReportingBranchInfoList));

                                // Mark the new instance of ReportingBranchInfoList as writable as it inherits from MarketPulseReadOnlyBase
                                SetIsReadOnly(reportingBranchInfoList, false);

                                // Select the resultset from the database.
                                var reportingBranchInfoSelect = productionManager.DataContext.UspBranchSelectByClientGUIDAndCountryGUID(criteria.ClientGUID, criteria.CountryGUID, true);

                                // Iterate over each row and append the object to the ReportingBranchInfoList
                                foreach (var countryBranchResult in reportingBranchInfoSelect)
                                {
                                    // Create an empty instance of ReportingBranchInfo to be appended to the ReportingBranchInfoList
                                    var reportingBranchInfo = (ReportingBranchInfo)MethodCaller.CreateInstance(typeof(ReportingBranchInfo));

                                    // Load the base properties
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.GUIDProperty, countryBranchResult.BranchGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.ObsoleteProperty, countryBranchResult.Obsolete.Value);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.InsertDateTimeProperty, (SmartDate)countryBranchResult.InsertDateTime);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.InsertUserGUIDProperty, countryBranchResult.InsertUserGUID.Value);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.UpdateDateTimeProperty, (SmartDate)countryBranchResult.UpdateDateTime);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.UpdateUserGUIDProperty, countryBranchResult.UpdateUserGUID.Value);

                                    // Load the ReportingBranchInfo specific properties
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.ClientGUIDProperty, criteria.ClientGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AreaGUIDProperty, countryBranchResult.AreaGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.CompanyGUIDProperty, countryBranchResult.CompanyGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.LegacyBranchIDProperty, countryBranchResult.LegacyBranchID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.BranchNameProperty, countryBranchResult.BranchName);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.BuildingNameOrNumberProperty, countryBranchResult.BuildingNameOrNumber);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine1Property, countryBranchResult.AddressLine1);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine2Property, countryBranchResult.AddressLine2);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine3Property, countryBranchResult.AddressLine3);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine4Property, countryBranchResult.AddressLine4);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine5Property, countryBranchResult.AddressLine5);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.PostCodeProperty, countryBranchResult.PostCode);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.MemoProperty, countryBranchResult.Memo);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.LatitudeProperty, countryBranchResult.Latitude);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.LongitudeProperty, countryBranchResult.Longtitude);

                                    // Go to the production database and get all the visits that we're interested in for this branch.
                                    var reportingVisitInfoList = (ReportingVisitInfoList)MethodCaller.CreateInstance(typeof(ReportingVisitInfoList));
                                    SetIsReadOnly(reportingVisitInfoList, false);

                                    var branchVisitSelectResult = productionManager.DataContext.UspVisitSelectByDateRangeAndBranchGUIDList(criteria.DataStartDateTime
                                                                                                                                         , criteria.DataEndDateTime
                                                                                                                                         , countryBranchResult.BranchGUID.ToString());

                                    foreach (var branchVisitResult in branchVisitSelectResult)
                                    {
                                        // Check to make sure that our Company reporting level procedure hasn't already added this visit.
                                        if (reportingVisitInfoList.FirstOrDefault(v => v.GUID.Value.Equals(branchVisitResult.VisitGUID.Value)) != null) continue;

                                        var reportingVisitInfo = (ReportingVisitInfo)MethodCaller.CreateInstance(typeof(ReportingVisitInfo));

                                        // Load ClientGUID from criteria
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.ClientGUIDProperty, criteria.ClientGUID);

                                        // Load GUID and base from production Visit, everything else from reporting Visit
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.GUIDProperty, branchVisitResult.VisitGUID);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.BranchGUIDProperty, branchVisitResult.BranchGUID.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.EffectiveRoundStartDateTimeProperty, (SmartDate)branchVisitResult.EffectiveStartDateTime.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.EffectiveRoundEndDateTimeProperty, (SmartDate)branchVisitResult.EffectiveEndDateTime.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.ComponentLabelProperty, branchVisitResult.ComponentLabel);
                                        // Common properties inherited from MarketPulseBase<Visit>
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.ObsoleteProperty, branchVisitResult.Obsolete.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.InsertDateTimeProperty, branchVisitResult.InsertDateTime.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.InsertUserGUIDProperty, branchVisitResult.InsertUserGUID.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.UpdateDateTimeProperty, branchVisitResult.UpdateDateTime.Value);
                                        LoadProperty(reportingVisitInfo, ReportingVisitInfo.UpdateUserGUIDProperty, branchVisitResult.UpdateUserGUID.Value);

                                        // Let Csla know that this object has changed
                                        MarkOld(reportingVisitInfo);

                                        // Add this ReportingVisitInfo object to the ReportingVisitInfoList
                                        reportingVisitInfoList.Add(reportingVisitInfo);
                                    }

                                    // Then go to the reporting database for each of those visits and get the reporting information
                                    foreach (var reportingVisitInfo in reportingVisitInfoList)
                                    {
                                        // TODO: Add/Update 'Published' parameter.
                                        var reportingManagerSelect = reportingManager.DataContext.UspReportingVisitSelectByClientGUIDAndVisitGUID(criteria.ClientGUID, reportingVisitInfo.GUID.Value, criteria.Language, false);

                                        // TODO: Data is dirty.. something to be done about it?
                                        if (reportingManagerSelect == null) continue;

                                        ReportingQuestionInfo lastReportingQuestionInfo = null;

                                        foreach (var reportingManagerSelectResult in reportingManagerSelect)
                                        {
                                            // If this is the first iteration of this visit
                                            if (!reportingVisitInfo.ID.HasValue)
                                            {
                                                // Other properties                    
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.VisitIDProperty, reportingManagerSelectResult.VisitID);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.VisitVersionIDProperty, reportingManagerSelectResult.VisitVersionID.Value);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.RoundGUIDProperty, reportingManagerSelectResult.RoundGUID);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.UserGUIDProperty, reportingManagerSelectResult.UserGUID);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ComponentGUIDProperty, reportingManagerSelectResult.ComponentGUID);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.CountryGUIDProperty, reportingManagerSelectResult.CountryGUID);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.LanguageGUIDProperty, reportingManagerSelectResult.LanguageGUID);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.LabelProperty, reportingManagerSelectResult.VisitLabel);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.PossibleScoreProperty, reportingManagerSelectResult.PossibleScore.Value);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ActualScoreProperty, reportingManagerSelectResult.ActualScore.Value);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.PublishedProperty, reportingManagerSelectResult.Published.Value);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.TransferredDateTimeProperty, reportingManagerSelectResult.TransferDateTime);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ValidStartDateTimeProperty, reportingManagerSelectResult.ValidStartDateTime);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ValidEndDateTimeProperty, reportingManagerSelectResult.ValidEndDateTime);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.PlannedDateTimeProperty, reportingManagerSelectResult.PlannedDateTime);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ActualStartDateTimeProperty, reportingManagerSelectResult.ActualStartDateTime);
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ActualEndDateTimeProperty, reportingManagerSelectResult.ActualEndDateTime);

                                                // Populate the first question row.
                                                var reportingQuestionInfoList = (ReportingQuestionInfoList)MethodCaller.CreateInstance(typeof(ReportingQuestionInfoList));
                                                SetIsReadOnly(reportingQuestionInfoList, false);

                                                var reportingQuestionInfo = (ReportingQuestionInfo)MethodCaller.CreateInstance(typeof(ReportingQuestionInfo));
                                                // Base properties
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.GUIDProperty, reportingManagerSelectResult.QuestionGUID.Value);
                                                // Extended properties
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.IDProperty, reportingManagerSelectResult.QuestionID.Value);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ClientGUIDProperty, criteria.ClientGUID);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.VisitGUIDProperty, reportingManagerSelectResult.VisitGUID.Value);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryGUIDProperty, reportingManagerSelectResult.CategoryGUID.Value);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryOrderProperty, reportingManagerSelectResult.CategoryOrder.Value);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryTitleProperty, reportingManagerSelectResult.CategoryTitle);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.DisplayNumberProperty, reportingManagerSelectResult.DisplayNumber);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.NumberProperty, reportingManagerSelectResult.QuestionNumber);
                                                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TextProperty, reportingManagerSelectResult.QuestionText);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TypeProperty, reportingManagerSelectResult.QuestionType);
                                                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.StandardProperty, reportingManagerSelectResult.QuestionStandard);
                                                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ShopperNoteProperty, reportingManagerSelectResult.ShopperNote);
                                                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.QANoteProperty, reportingManagerSelectResult.QANote);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.PossibleScoreProperty, reportingManagerSelectResult.QuestionPossibleScore.Value);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ActualScoreProperty, reportingManagerSelectResult.QuestionActualScore.Value);
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TransferDateTimeProperty, reportingManagerSelectResult.TransferDateTime);

                                                // Initalise the question references property.
                                                var questionReferences = new List<string>();
                                                if (!String.IsNullOrEmpty(reportingManagerSelectResult.QuestionReferenceTypeShortName))
                                                {
                                                    questionReferences.Add(reportingManagerSelectResult.QuestionReferenceTypeShortName);
                                                }
                                                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ReferencesProperty, questionReferences);

                                                // Let Csla know this object has changed
                                                MarkOld(reportingQuestionInfo);

                                                // Add the question to the collection
                                                reportingQuestionInfoList.Add(reportingQuestionInfo);

                                                // Save this question for appending to later.
                                                lastReportingQuestionInfo = reportingQuestionInfo;

                                                // Set the ReportingQuestionInfoList to read-only now
                                                SetIsReadOnly(reportingQuestionInfoList, true);

                                                // Set ReportingQuestionInfoList onto the ReportingVisitInfoList
                                                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ReportingQuestionsProperty, reportingQuestionInfoList);
                                            }
                                            else
                                            {
                                                // Not the first iteration of this visit, add/update question as required.
                                                if (lastReportingQuestionInfo == null || !lastReportingQuestionInfo.GUID.Value.Equals(reportingManagerSelectResult.QuestionGUID.Value))
                                                {
                                                    // Add new question
                                                    var reportingQuestionInfo = (ReportingQuestionInfo)MethodCaller.CreateInstance(typeof(ReportingQuestionInfo));
                                                    // Base properties
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.GUIDProperty, reportingManagerSelectResult.QuestionGUID.Value);
                                                    // Extended properties
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.IDProperty, reportingManagerSelectResult.QuestionID.Value);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ClientGUIDProperty, criteria.ClientGUID);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.VisitGUIDProperty, reportingManagerSelectResult.VisitGUID.Value);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryGUIDProperty, reportingManagerSelectResult.CategoryGUID.Value);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryOrderProperty, reportingManagerSelectResult.CategoryOrder.Value);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryTitleProperty, reportingManagerSelectResult.CategoryTitle);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.DisplayNumberProperty, reportingManagerSelectResult.DisplayNumber);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.NumberProperty, reportingManagerSelectResult.QuestionNumber);
                                                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TextProperty, reportingManagerSelectResult.QuestionText);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TypeProperty, reportingManagerSelectResult.QuestionType);
                                                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.StandardProperty, reportingManagerSelectResult.QuestionStandard);
                                                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ShopperNoteProperty, reportingManagerSelectResult.ShopperNote);
                                                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.QANoteProperty, reportingManagerSelectResult.QANote);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.PossibleScoreProperty, reportingManagerSelectResult.QuestionPossibleScore.Value);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ActualScoreProperty, reportingManagerSelectResult.QuestionActualScore.Value);
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TransferDateTimeProperty, reportingManagerSelectResult.TransferDateTime);

                                                    // Initalise the question references property.
                                                    var questionReferences = new List<string>();
                                                    if (!String.IsNullOrEmpty(reportingManagerSelectResult.QuestionReferenceTypeShortName))
                                                    {
                                                        questionReferences.Add(reportingManagerSelectResult.QuestionReferenceTypeShortName);
                                                    }
                                                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ReferencesProperty, questionReferences);

                                                    // Let Csla know this object has changed
                                                    MarkOld(reportingQuestionInfo);

                                                    // Use our existing question collection and set it as read-only
                                                    SetIsReadOnly(reportingVisitInfo.ReportingQuestions, false);

                                                    // Append the new question
                                                    reportingVisitInfo.ReportingQuestions.Add(reportingQuestionInfo);

                                                    // Set the question collection back to read only
                                                    SetIsReadOnly(reportingVisitInfo.ReportingQuestions, true);

                                                    lastReportingQuestionInfo = reportingQuestionInfo;
                                                }
                                                else
                                                {
                                                    // Update current question
                                                    if (!String.IsNullOrEmpty(reportingManagerSelectResult.QuestionReferenceTypeShortName))
                                                    {
                                                        // We have new references to append to the existing question.
                                                        lastReportingQuestionInfo.References.Add(reportingManagerSelectResult.QuestionReferenceTypeShortName);

                                                        // Let Csla know this object has changed.
                                                        MarkOld(lastReportingQuestionInfo);
                                                    }
                                                }
                                            }
                                        }

                                        // Let Csla this object has changed since instantiation.
                                        MarkOld(reportingVisitInfo);
                                    }

                                    // Let Csla know this object has changed since instantiation.
                                    MarkOld(reportingVisitInfoList);


                                    // Fill Results
                                    //Results<double> branchResults = ResultsFactory.Create(new ResultsCriteria(criteria.ClientGUID.Value, countryBranchResult.BranchGUID.Value, criteria.Language, criteria.CurrentStartDateTime, criteria.CurrentEndDateTime, criteria.DataStartDateTime, criteria.DataEndDateTime), reportingVisitInfoList);
                                    //branchResults.Calculate();
                                    //LoadProperty(reportingBranchInfo, ReportingBranchInfo.ResultsProperty, branchResults);

                                    // Mark the ReportingBranchInfo object as old so CSLA knows it has changed
                                    MarkOld(reportingBranchInfo);

                                    // Add the ReportingBranchInfo object to the ReportingBranchInfoList collection
                                    reportingBranchInfoList.Add(reportingBranchInfo);
                                }

                                // Mark the new instance of ReportingBranchInfoList as read-only before returning it to the client
                                SetIsReadOnly(reportingBranchInfoList, true);

                                // Mark the ReportingBranchInfoList object as old so CSLA knows it has changed
                                MarkOld(reportingBranchInfoList);

                                // Return the newly populated ReportingBranchInfoList
                                return reportingBranchInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            //TODO log exception appropriately before re throwing
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

                    /// <summary>
                    /// Method used to get a read only list of read only Reporting Branches that are associated with a particular set of criteria (via the DataPortal client proxy)
                    /// </summary>
                    /// <param name="criteria">The criteria to use to retrieve the list</param>
                    /// <returns>A populated read only list of read only Reporting Branches</returns>
                    public ReportingBranchInfoList FetchInfoList(ClientGUIDAndProfileGUIDCriteria criteria)
                    {
                        try
                        {
                            using (var productionManager = ContextManager<DataContextObjects.Production.MUM_dev>.GetManager(Database.BusinessSystem))
                            using (var reportingManager = ContextManager<DataContextObjects.Reporting.MUMReporting_dev>.GetManager(Database.ReportingSystem))
                            {
                                // Create an empty instance of ReportingBranchInfoList to be returned.
                                var reportingBranchInfoList = (ReportingBranchInfoList)MethodCaller.CreateInstance(typeof(ReportingBranchInfoList));

                                // Mark the new instance of ReportingBranchInfoList as writable as it inherits from MarketPulseReadOnlyBase
                                SetIsReadOnly(reportingBranchInfoList, false);

                                // Select the resultset from the database.
                                var reportingBranchInfoSelect = productionManager.DataContext.UspBranchSelectByProfileGUIDAndClientGUID(criteria.ProfileGUID, criteria.ClientGUID);

                                // Iterate over each row and append the object to the ReportingBranchInfoList
                                foreach (var countryBranchResult in reportingBranchInfoSelect)
                                {
                                    // Create an empty instance of ReportingBranchInfo to be appended to the ReportingBranchInfoList
                                    var reportingBranchInfo = (ReportingBranchInfo)MethodCaller.CreateInstance(typeof(ReportingBranchInfo));

                                    // Load the base properties
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.GUIDProperty, countryBranchResult.BranchGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.ObsoleteProperty, countryBranchResult.Obsolete.Value);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.InsertDateTimeProperty, (SmartDate)countryBranchResult.InsertDateTime);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.InsertUserGUIDProperty, countryBranchResult.InsertUserGUID.Value);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.UpdateDateTimeProperty, (SmartDate)countryBranchResult.UpdateDateTime);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.UpdateUserGUIDProperty, countryBranchResult.UpdateUserGUID.Value);

                                    // Load the ReportingBranchInfo specific properties
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.ClientGUIDProperty, criteria.ClientGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AreaGUIDProperty, countryBranchResult.AreaGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.CompanyGUIDProperty, countryBranchResult.CompanyGUID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.LegacyBranchIDProperty, countryBranchResult.LegacyBranchID);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.BranchNameProperty, countryBranchResult.BranchName);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.BuildingNameOrNumberProperty, countryBranchResult.BuildingNameOrNumber);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine1Property, countryBranchResult.AddressLine1);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine2Property, countryBranchResult.AddressLine2);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine3Property, countryBranchResult.AddressLine3);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine4Property, countryBranchResult.AddressLine4);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.AddressLine5Property, countryBranchResult.AddressLine5);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.PostCodeProperty, countryBranchResult.PostCode);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.MemoProperty, countryBranchResult.Memo);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.LatitudeProperty, countryBranchResult.Latitude);
                                    LoadProperty(reportingBranchInfo, ReportingBranchInfo.LongitudeProperty, countryBranchResult.Longtitude);

                                    // Go to the production database and get all the visits that we're interested in for this branch.
                                    //var reportingVisitInfoList = (ReportingVisitInfoList)MethodCaller.CreateInstance(typeof(ReportingVisitInfoList));
                                    //SetIsReadOnly(reportingVisitInfoList, false);

                                    //var branchVisitSelectResult = productionManager.DataContext.UspVisitSelectByDateRangeAndBranchGUIDList(criteria.DataStartDateTime
                                    //                                                                                                     , criteria.DataEndDateTime
                                    //                                                                                                     , countryBranchResult.BranchGUID.ToString());

                                    //foreach (var branchVisitResult in branchVisitSelectResult)
                                    //{
                                    //    // Check to make sure that our Company reporting level procedure hasn't already added this visit.
                                    //    if (reportingVisitInfoList.FirstOrDefault(v => v.GUID.Value.Equals(branchVisitResult.VisitGUID.Value)) != null) continue;

                                    //    var reportingVisitInfo = (ReportingVisitInfo)MethodCaller.CreateInstance(typeof(ReportingVisitInfo));

                                    //    // Load ClientGUID from criteria
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.ClientGUIDProperty, criteria.ClientGUID);

                                    //    // Load GUID and base from production Visit, everything else from reporting Visit
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.GUIDProperty, branchVisitResult.VisitGUID);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.BranchGUIDProperty, branchVisitResult.BranchGUID.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.EffectiveRoundStartDateTimeProperty, (SmartDate)branchVisitResult.EffectiveStartDateTime.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.EffectiveRoundEndDateTimeProperty, (SmartDate)branchVisitResult.EffectiveEndDateTime.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.ComponentLabelProperty, branchVisitResult.ComponentLabel);
                                    //    // Common properties inherited from MarketPulseBase<Visit>
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.ObsoleteProperty, branchVisitResult.Obsolete.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.InsertDateTimeProperty, branchVisitResult.InsertDateTime.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.InsertUserGUIDProperty, branchVisitResult.InsertUserGUID.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.UpdateDateTimeProperty, branchVisitResult.UpdateDateTime.Value);
                                    //    LoadProperty(reportingVisitInfo, ReportingVisitInfo.UpdateUserGUIDProperty, branchVisitResult.UpdateUserGUID.Value);

                                    //    // Let Csla know that this object has changed
                                    //    MarkOld(reportingVisitInfo);

                                    //    // Add this ReportingVisitInfo object to the ReportingVisitInfoList
                                    //    reportingVisitInfoList.Add(reportingVisitInfo);
                                    //}

                                //    // Then go to the reporting database for each of those visits and get the reporting information
                                //    foreach (var reportingVisitInfo in reportingVisitInfoList)
                                //    {
                                //        // TODO: Add/Update 'Published' parameter.
                                //        var reportingManagerSelect = reportingManager.DataContext.UspReportingVisitSelectByClientGUIDAndVisitGUID(criteria.ClientGUID, reportingVisitInfo.GUID.Value, criteria.Language, false);

                                //        // TODO: Data is dirty.. something to be done about it?
                                //        if (reportingManagerSelect == null) continue;

                                //        ReportingQuestionInfo lastReportingQuestionInfo = null;

                                //        foreach (var reportingManagerSelectResult in reportingManagerSelect)
                                //        {
                                //            // If this is the first iteration of this visit
                                //            if (!reportingVisitInfo.ID.HasValue)
                                //            {
                                //                // Other properties
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.VisitIDProperty, reportingManagerSelectResult.VisitID);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.VisitVersionIDProperty, reportingManagerSelectResult.VisitVersionID.Value);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.RoundGUIDProperty, reportingManagerSelectResult.RoundGUID);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.UserGUIDProperty, reportingManagerSelectResult.UserGUID);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ComponentGUIDProperty, reportingManagerSelectResult.ComponentGUID);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.CountryGUIDProperty, reportingManagerSelectResult.CountryGUID);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.LanguageGUIDProperty, reportingManagerSelectResult.LanguageGUID);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.LabelProperty, reportingManagerSelectResult.VisitLabel);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.PossibleScoreProperty, reportingManagerSelectResult.PossibleScore.Value);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ActualScoreProperty, reportingManagerSelectResult.ActualScore.Value);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.PublishedProperty, reportingManagerSelectResult.Published.Value);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.TransferredDateTimeProperty, reportingManagerSelectResult.TransferDateTime);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ValidStartDateTimeProperty, reportingManagerSelectResult.ValidStartDateTime);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ValidEndDateTimeProperty, reportingManagerSelectResult.ValidEndDateTime);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.PlannedDateTimeProperty, reportingManagerSelectResult.PlannedDateTime);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ActualStartDateTimeProperty, reportingManagerSelectResult.ActualStartDateTime);
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ActualEndDateTimeProperty, reportingManagerSelectResult.ActualEndDateTime);

                                //                // Populate the first question row.
                                //                var reportingQuestionInfoList = (ReportingQuestionInfoList)MethodCaller.CreateInstance(typeof(ReportingQuestionInfoList));
                                //                SetIsReadOnly(reportingQuestionInfoList, false);

                                //                var reportingQuestionInfo = (ReportingQuestionInfo)MethodCaller.CreateInstance(typeof(ReportingQuestionInfo));
                                //                // Base properties
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.GUIDProperty, reportingManagerSelectResult.QuestionGUID.Value);
                                //                // Extended properties
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.IDProperty, reportingManagerSelectResult.QuestionID.Value);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ClientGUIDProperty, criteria.ClientGUID);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.VisitGUIDProperty, reportingManagerSelectResult.VisitGUID.Value);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryGUIDProperty, reportingManagerSelectResult.CategoryGUID.Value);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryOrderProperty, reportingManagerSelectResult.CategoryOrder.Value);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryTitleProperty, reportingManagerSelectResult.CategoryTitle);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.DisplayNumberProperty, reportingManagerSelectResult.DisplayNumber);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.NumberProperty, reportingManagerSelectResult.QuestionNumber);
                                //                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TextProperty, reportingManagerSelectResult.QuestionText);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TypeProperty, reportingManagerSelectResult.QuestionType);
                                //                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.StandardProperty, reportingManagerSelectResult.QuestionStandard);
                                //                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ShopperNoteProperty, reportingManagerSelectResult.ShopperNote);
                                //                //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.QANoteProperty, reportingManagerSelectResult.QANote);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.PossibleScoreProperty, reportingManagerSelectResult.QuestionPossibleScore.Value);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ActualScoreProperty, reportingManagerSelectResult.QuestionActualScore.Value);
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TransferDateTimeProperty, reportingManagerSelectResult.TransferDateTime);

                                //                // Initalise the question references property.
                                //                var questionReferences = new List<string>();
                                //                if (!String.IsNullOrEmpty(reportingManagerSelectResult.QuestionReferenceTypeShortName))
                                //                {
                                //                    questionReferences.Add(reportingManagerSelectResult.QuestionReferenceTypeShortName);
                                //                }
                                //                LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ReferencesProperty, questionReferences);

                                //                // Let Csla know this object has changed
                                //                MarkOld(reportingQuestionInfo);

                                //                // Add the question to the collection
                                //                reportingQuestionInfoList.Add(reportingQuestionInfo);

                                //                // Save this question for appending to later.
                                //                lastReportingQuestionInfo = reportingQuestionInfo;

                                //                // Set the ReportingQuestionInfoList to read-only now
                                //                SetIsReadOnly(reportingQuestionInfoList, true);

                                //                // Set ReportingQuestionInfoList onto the ReportingVisitInfoList
                                //                LoadProperty(reportingVisitInfo, ReportingVisitInfo.ReportingQuestionsProperty, reportingQuestionInfoList);
                                //            }
                                //            else
                                //            {
                                //                // Not the first iteration of this visit, add/update question as required.
                                //                if (lastReportingQuestionInfo == null || !lastReportingQuestionInfo.GUID.Value.Equals(reportingManagerSelectResult.QuestionGUID.Value))
                                //                {
                                //                    // Add new question
                                //                    var reportingQuestionInfo = (ReportingQuestionInfo)MethodCaller.CreateInstance(typeof(ReportingQuestionInfo));
                                //                    // Base properties
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.GUIDProperty, reportingManagerSelectResult.QuestionGUID.Value);
                                //                    // Extended properties
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.IDProperty, reportingManagerSelectResult.QuestionID.Value);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ClientGUIDProperty, criteria.ClientGUID);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.VisitGUIDProperty, reportingManagerSelectResult.VisitGUID.Value);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryGUIDProperty, reportingManagerSelectResult.CategoryGUID.Value);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryOrderProperty, reportingManagerSelectResult.CategoryOrder.Value);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.CategoryTitleProperty, reportingManagerSelectResult.CategoryTitle);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.DisplayNumberProperty, reportingManagerSelectResult.DisplayNumber);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.NumberProperty, reportingManagerSelectResult.QuestionNumber);
                                //                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TextProperty, reportingManagerSelectResult.QuestionText);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TypeProperty, reportingManagerSelectResult.QuestionType);
                                //                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.StandardProperty, reportingManagerSelectResult.QuestionStandard);
                                //                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ShopperNoteProperty, reportingManagerSelectResult.ShopperNote);
                                //                    //LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.QANoteProperty, reportingManagerSelectResult.QANote);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.PossibleScoreProperty, reportingManagerSelectResult.QuestionPossibleScore.Value);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ActualScoreProperty, reportingManagerSelectResult.QuestionActualScore.Value);
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.TransferDateTimeProperty, reportingManagerSelectResult.TransferDateTime);

                                //                    // Initalise the question references property.
                                //                    var questionReferences = new List<string>();
                                //                    if (!String.IsNullOrEmpty(reportingManagerSelectResult.QuestionReferenceTypeShortName))
                                //                    {
                                //                        questionReferences.Add(reportingManagerSelectResult.QuestionReferenceTypeShortName);
                                //                    }
                                //                    LoadProperty(reportingQuestionInfo, ReportingQuestionInfo.ReferencesProperty, questionReferences);

                                //                    // Let Csla know this object has changed
                                //                    MarkOld(reportingQuestionInfo);

                                //                    // Use our existing question collection and set it as read-only
                                //                    SetIsReadOnly(reportingVisitInfo.ReportingQuestions, false);

                                //                    // Append the new question
                                //                    reportingVisitInfo.ReportingQuestions.Add(reportingQuestionInfo);

                                //                    // Set the question collection back to read only
                                //                    SetIsReadOnly(reportingVisitInfo.ReportingQuestions, true);

                                //                    lastReportingQuestionInfo = reportingQuestionInfo;
                                //                }
                                //                else
                                //                {
                                //                    // Update current question
                                //                    if (!String.IsNullOrEmpty(reportingManagerSelectResult.QuestionReferenceTypeShortName))
                                //                    {
                                //                        // We have new references to append to the existing question.
                                //                        lastReportingQuestionInfo.References.Add(reportingManagerSelectResult.QuestionReferenceTypeShortName);

                                //                        // Let Csla know this object has changed.
                                //                        MarkOld(lastReportingQuestionInfo);
                                //                    }
                                //                }
                                //            }
                                //        }

                                //        // Let Csla this object has changed since instantiation.
                                //        MarkOld(reportingVisitInfo);
                                //    }

                                //    // Let Csla know this object has changed since instantiation.
                                //    MarkOld(reportingVisitInfoList);


                                //    // Fill Results
                                //    Results<double> branchResults = ResultsFactory.Create(new ResultsCriteria(criteria.ClientGUID.Value, countryBranchResult.BranchGUID.Value, criteria.Language, criteria.CurrentStartDateTime, criteria.CurrentEndDateTime, criteria.DataStartDateTime, criteria.DataEndDateTime), reportingVisitInfoList);
                                //    branchResults.Calculate();
                                //    LoadProperty(reportingBranchInfo, ReportingBranchInfo.ResultsProperty, branchResults);

                                //  Mark the ReportingBranchInfo object as old so CSLA knows it has changed
                                    MarkOld(reportingBranchInfo);

                                //  Add the ReportingBranchInfo object to the ReportingBranchInfoList collection
                                    reportingBranchInfoList.Add(reportingBranchInfo);
                                }

                                // Mark the new instance of ReportingBranchInfoList as read-only before returning it to the client
                                SetIsReadOnly(reportingBranchInfoList, true);

                                // Mark the ReportingBranchInfoList object as old so CSLA knows it has changed
                                MarkOld(reportingBranchInfoList);

                                // Return the newly populated ReportingBranchInfoList
                                return reportingBranchInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            //TODO log exception appropriately before re throwing
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

                #endregion

            #endregion

        #endregion

    }
}
